package com.bdqn.crm.repository;

import com.bdqn.crm.utils.DataFormat;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ShiroTester {


    @Test
    public void testShiro() {
        //1.创建Realm(安全数据源)
        //通过shiro.ini配置文件创建Realm
        IniRealm realm = new IniRealm("classpath:shiro.ini");
        //2.配置SecurityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        //注入创建的Realm(安全数据源)
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);

        //3.操作Subject,进行认证
        Subject subject = SecurityUtils.getSubject();
        //封装一个令牌
        UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456");
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            System.out.println("认证异常:");
            e.printStackTrace();
        }
//        System.out.println("是否认证通过:"+subject.isAuthenticated());
//        System.out.println("身份信息:"+subject.getPrincipal());

        System.out.println("是否认证通过" + subject.isAuthenticated());
        //认证通过后，进行权限验证
        System.out.println("是否为管理员角色:" + subject.hasRole("管理员"));//判断是否为某角色
        System.out.println("是否能操作用户查看功能:" + subject.isPermitted("user:view"));    //判断是否拥有某权限

        //也可使用check方法判断是否拥有某权限，但是失败的情况下会抛出 UnauthorizedException 异常
        subject.checkPermission("user:view");


    }

    /**
     * jvm堆内存泄漏用例
     */
    @Test
    public void testMain() {
        int i = 0;
        List<String> list = new ArrayList<>();
        String a = "hello";
        try {
            while (true) {
                list.add(a);
                a = a + a;
                i++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println(i);
        }

    }

    /**
     * jvm堆内存诊断
     */
    @Test
    public void testDemo() throws InterruptedException {
        System.out.println("1...");
        Thread.sleep(30000);
        byte[] array = new byte[1024 * 1024 * 10];  //10mb
        System.out.println("2....");
        Thread.sleep(30000);
        array = null;
        System.gc();
        System.out.println("3....");
        Thread.sleep(1000000L);
    }

    @Test
    public void testDate(){
        String strDate = "2021-04-06 09:45:43";
        System.out.println(DataFormat.forMat(strDate));

    }

}
